package ldh.msg;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;

import ldh.msg.helper.AbsentChecker;
import ldh.msg.helper.MsgConstants;
import ldh.msg.ui.helper.MsgSndPlayer;

public class MsgConfigInfo {
//	private static String hostIp = null;	
    Properties props;
    
    private static MsgConfigInfo info = null;
    
    private MsgConfigInfo() throws Exception {
        props = new Properties();
        String hostName = null;
        try {
//        	hostName = MsgManager.getInstance().getHostName();
        	hostName = "Whoami?";
        } catch (Exception e) {
        	hostName = "Whoami?";
        }
        
    	if (fileCheck())
        props.load(new FileInputStream(System.getProperty("user.home") + "/.msgshooter/conf/msg.properties"));
    	if (props.isEmpty()) {
    		props.setProperty("grp", "");//grp
    		props.setProperty("name", hostName);//name
    		props.setProperty("keepSLAS", "false");//keepSLAS
    		props.setProperty("keepRM", "");//keepRM
    		props.setProperty("keepSM", "");//keepSM
    		props.setProperty("notiInterV", "1");//notiInterV
    		props.setProperty("absent", "0");//absent
    		props.setProperty("absentMsg", "false");//absentMsg
    		props.setProperty("sound", "true");//sound
    		props.setProperty("generalSnd", "0");//generalSnd
    		props.setProperty("grpList", "");//grpList
    		props.setProperty("sndVol", "20");//sndVol
    		props.setProperty("sndList", "");//sndList
    		props.setProperty("nwList", "");//nwList
    		props.setProperty("nwGrp", "0");//nwGrp
    	} else {
    		if (props.getProperty("grp") == null || props.getProperty("grp").equals(""))  props.setProperty("grp", "");//grp
    		if (props.getProperty("name") == null || props.getProperty("name").equals(""))  props.setProperty("name", hostName);//name
    		if (props.getProperty("keepSLAS") == null || props.getProperty("keepSLAS").equals(""))  props.setProperty("keepSLAS", "false");//keepSLAS
    		if (props.getProperty("keepRM") == null || props.getProperty("keepRM").equals(""))  props.setProperty("keepRM", "");//keepRM
    		if (props.getProperty("keepSM") == null || props.getProperty("keepSM").equals(""))  props.setProperty("keepSM", "");//keepSM
    		if (props.getProperty("notiInterV") == null || props.getProperty("notiInterV").equals(""))  props.setProperty("notiInterV", "1");//notiInterV
    		if (props.getProperty("absent") == null || props.getProperty("absent").equals(""))  props.setProperty("absent", "0");//absent
    		if (props.getProperty("absentMsg") == null || props.getProperty("absentMsg").equals(""))  props.setProperty("absentMsg", "false");//absentMsg
    		if (props.getProperty("sound") == null || props.getProperty("sound").equals(""))  props.setProperty("sound", "true");//sound
    		if (props.getProperty("generalSnd") == null || props.getProperty("generalSnd").equals(""))  props.setProperty("generalSnd", "0");//sound
    		if (props.getProperty("grpList") == null || props.getProperty("grpList").equals(""))  props.setProperty("grpList", "");//grpList
    		if (props.getProperty("sndVol") == null || props.getProperty("sndVol").equals(""))  props.setProperty("sndVol", "20");//sndVol
    		if (props.getProperty("sndList") == null || props.getProperty("sndList").equals(""))  props.setProperty("sndList", "");//sndList
    		if (props.getProperty("nwList") == null || props.getProperty("nwList").equals(""))  props.setProperty("nwList", "");//sndList
    		if (props.getProperty("nwGrp") == null || props.getProperty("nwGrp").equals(""))  props.setProperty("nwGrp", "0");//sndList
    	}
    	outputConfFile();
    }
    
    public static MsgConfigInfo getInstance() throws Exception {
    	if (info == null) {
			synchronized (MsgConfigInfo.class) {
				info = new MsgConfigInfo();
			}
		}
		return info;
    }
    public void setProperties(String key, String value) throws Exception {
		props.setProperty(key, value);
		outputConfFile();
	}
    
    public void getProperties(String key) {
		props.getProperty(key);
	}
    
	public String getGrp(){
		return props.getProperty("grp");
	}
	
	public void setGrp(String value) throws Exception {
		props.setProperty("grp", value);
		outputConfFile();
	}
	
	public String getName() {
		return props.getProperty("name");
	}
	
	public void setName(String value) throws Exception {
		props.setProperty("name", value);
		outputConfFile();
	}
	
	public boolean isKeepSubsListAfterSend(){
		if (props.getProperty("keepSLAS") != null) {
			if (props.getProperty("keepSLAS").equals("true")) {
				return true;
			}
		}
		return false;
	}
	
	public void setKeepSubsListAfterSend(String value) throws Exception {
		props.setProperty("keepSLAS", value);
		outputConfFile();
	}
	
	public int[] getKeepRecvMsgList(){
		int[] msgList = null;
		ArrayList<Integer> intArr = new ArrayList<Integer>();
		if (props.getProperty("keepRM") != null) {			
			String[] msgStrList = props.getProperty("keepRM").split(",");
			for (int i = 0 ; i < msgStrList.length ; i++) {
				try {
					if (! msgStrList[i].equals("")) {
						intArr.add(Integer.parseInt(msgStrList[i]));
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		if (intArr.size() > 0) {
			msgList = new int[intArr.size()];
			for (int i = 0 ; i < intArr.size()  ; i++) {
				msgList[i] = intArr.get(i);
			}
		}
		return msgList;
	}
	
	public int[] getKeepSendMsgList(){
		int[] msgList = null;
		ArrayList<Integer> intArr = new ArrayList<Integer>();
		if (props.getProperty("keepSM") != null) {			
			String[] msgStrList = props.getProperty("keepSM").split(",");
			for (int i = 0 ; i < msgStrList.length ; i++) {
				try {
					if (! msgStrList[i].equals("")) {
						intArr.add(Integer.parseInt(msgStrList[i]));
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		if (intArr.size() > 0) {
			msgList = new int[intArr.size()];
			for (int i = 0 ; i < intArr.size()  ; i++) {
				msgList[i] = intArr.get(i);
			}
		}
		return msgList;
	}
	
	public void setKeepRecvMsgList(String value) throws Exception {
		props.setProperty("keepRM", value);
		outputConfFile();
	}
	
	public void setKeepSendMsgList(String value) throws Exception {
		props.setProperty("keepSM", value);
		outputConfFile();
	}
	
	public int getNotiInterV() throws Exception {
		int retInt = 0;
		try {
			retInt = Integer.parseInt(props.getProperty("notiInterV"));
		} catch (NumberFormatException nbfe) {
			nbfe.printStackTrace();
		}
		return retInt;
	}
	
	public void setNotiInterV(String value) throws Exception {
		props.setProperty("notiInterV", value);
		outputConfFile();
	}
	
	public int getAbsent() throws Exception {
		int retInt = 0;
		try {
			retInt = Integer.parseInt(props.getProperty("absent"));
		} catch (NumberFormatException nbfe) {
			nbfe.printStackTrace();
		}
		return retInt;
	}
	
	public void setAbsent(String value) throws Exception {
		if (! props.getProperty("absent").equals(value)) {
			if (value.equals("0")) {
				AbsentChecker.setOperFlag(false);
				AbsentChecker.getInstance().destroy();
			} else {
				AbsentChecker.setOperFlag(false);
//				AbsentChecker.setOperFlag(true);
//				Thread.sleep(2 * 1000);
				AbsentChecker.getInstance().destroy();
				AbsentChecker.getInstance().start();
			}
			props.setProperty("absent", value);
			outputConfFile();
		}
	}
	
	public boolean getAbsentMsg() throws Exception {
		if (props.getProperty("absentMsg") != null) {
			if (props.getProperty("absentMsg").equals("true")) {
				return true;
			}
		}
		return false;
	}
	
	public void setAbsentMsg(String value) throws Exception {
		props.setProperty("absentMsg", value);
		outputConfFile();
	}
	
	public boolean isSound() throws Exception {
		if (props.getProperty("sound") != null) {
			if (props.getProperty("sound").equals("true")) {
				return true;
			}
		}
		return false;
	}
	
	public void setSound(String value) throws Exception {
		props.setProperty("sound", value);
		outputConfFile();
	}
	
	public int getGeneralSnd() throws Exception {
		int retInt = 0;
		try {
			retInt = Integer.parseInt(props.getProperty("generalSnd"));
		} catch (NumberFormatException nbfe) {
			nbfe.printStackTrace();
		}
		return retInt;
	}
	
	public void setGeneralSnd(String value) throws Exception {
		props.setProperty("generalSnd", value);
		outputConfFile();
	}
	
	public String getGrpSnd(String grpName) {
		ArrayList<String> grpListArr = new ArrayList<String>();
		String propsStr = null;
		String[] tempGrpList = null;
		if ((propsStr = props.getProperty("grpList")) != null) {
			tempGrpList = propsStr.split(",");
			if (tempGrpList != null && tempGrpList.length > 0) {
				String[] tempList = null;
				for (int i = 0 ; i < tempGrpList.length ; i++) {
					try {
						if (tempGrpList[i].contains(":")) {
							tempList = tempGrpList[i].split(":");
							if (tempList != null && tempList.length == 2 && ! tempList[0].equals("") && ! tempList[1].equals("")) {
								if (tempList[0].equals(grpName)) {
									return tempList[1];
								}
								grpListArr.add(tempList[0]);
							}
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
		return null;
	}
	
	public String[] getGrpList() {
		ArrayList<String> grpListArr = new ArrayList<String>();
		String propsStr = null;
		String[] tempGrpList = null;
		if ((propsStr = props.getProperty("grpList")) != null) {
			tempGrpList = propsStr.split(",");
			if (tempGrpList != null && tempGrpList.length > 0) {
				String[] tempList = null;
				for (int i = 0 ; i < tempGrpList.length ; i++) {
					try {
						if (tempGrpList[i].contains(":")) {
							tempList = tempGrpList[i].split(":");
							if (tempList != null && tempList.length == 2 && ! tempList[0].equals("") && ! tempList[1].equals("")) {
								grpListArr.add(tempList[0]);
							}
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
		return grpListArr.toArray(new String[grpListArr.size()]);
	}
	
	public String[] getGrpSndList() {
		String propsStr = null;
		String[] tempGrpList = null;
		if ((propsStr = props.getProperty("grpList")) != null) {
			tempGrpList = propsStr.split(",");			
		}
		return tempGrpList;
	}
	
	public void addGrpList(String value) throws Exception {		
		TreeMap<String, String> sndByGrpMap = new TreeMap<String, String>();
		String propsStr = null;
		if ((propsStr = props.getProperty("grpList")) != null) {
			String[] tempGrpList = propsStr.split(",");
			if (tempGrpList != null && tempGrpList.length > 0) {
				String[] tempList = null;
				for (int i = 0 ; i < tempGrpList.length ; i++) {
					try {
						if (tempGrpList[i].contains(":")) {
							tempList = tempGrpList[i].split(":");
							if (tempList != null && tempList.length == 2 && ! tempList[0].equals("") && ! tempList[1].equals("")) {
//								System.out.println("setGrpListMap   : [" + i + "] value : " + value + " , tempList[0] : " + tempList[0] + ", tempList[0] : " + tempList[1]);
								if (tempList[0].equals(value)) {
									return;
								}
								sndByGrpMap.put(tempList[0], tempList[1]);
							}
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
//		System.out.println("Before addGrpList : size() : " + sndByGrpMap.size() + ", value : " + value);
		sndByGrpMap.put(value, "T0");
//		System.out.println("After addGrpList : size() : " + sndByGrpMap.size() + ", value : " + value);
		setGrpListMap(sndByGrpMap);
	}
	
	public void setGrpListMap(TreeMap<String, String> sndByGrpMap) throws Exception {
		System.out.println("setGrpListMap : " + sndByGrpMap.size());
		if (sndByGrpMap != null && sndByGrpMap.size() > 0) {
			StringBuffer sb = new StringBuffer();
			String[] arrayKey = null;
			String[] arrayVal = null;
			
			Set<String> set = null;
			set = sndByGrpMap.keySet();
			
			arrayKey = new String[set.size()];
			set.toArray(arrayKey);
			
			Collection<String> al = new ArrayList<String>();			
			al = sndByGrpMap.values();
			
			arrayVal = new String[al.size()];
			al.toArray(arrayVal);
			
			for (int i = 0 ; i < arrayKey.length ; i++) {
//				System.out.println("setGrpListMap  : [" + i + "] Key : " + arrayKey[i] + ", Val : " + arrayVal[i]);
				sb.append(arrayKey[i] + ":" + arrayVal[i]);
				if (i < arrayKey.length - 1) sb.append(",");
			}
			System.out.println("setGrpListMap  grpList sb : " + sb.toString());
			props.setProperty("grpList", sb.toString());
			outputConfFile();
		}		
	}
	
	public TreeMap<String, String> getGrpListMap() {
		TreeMap<String, String> sndByGrpMap = new TreeMap<String, String>();
		String propsStr = null;
		if ((propsStr = props.getProperty("grpList")) != null) {
			String[] tempGrpList = propsStr.split(",");
			if (tempGrpList != null && tempGrpList.length > 0) {
				String[] tempList = null;
				for (int i = 0 ; i < tempGrpList.length ; i++) {
					try {
						if (tempGrpList[i].contains(":")) {
							tempList = tempGrpList[i].split(":");
							if (tempList != null && tempList.length == 2 && ! tempList[0].equals("") && ! tempList[1].equals("")) {
								sndByGrpMap.put(tempList[0], tempList[1]);
							}
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
		return sndByGrpMap;
	}
	
	public int getSndVol() throws Exception {
		int retInt = 0;
		try {
			retInt = Integer.parseInt(props.getProperty("sndVol"));
		} catch (NumberFormatException nbfe) {
			nbfe.printStackTrace();
		}
		return retInt;
	}
	
	public void setSndVol(int value) throws Exception {
		MsgSndPlayer.getInstance().setVolume(value);
		props.setProperty("sndVol", Integer.toString(value));
		outputConfFile();
	}
	
	public String[] getSndList() throws Exception {
		String[] sndList = null;
		try {
			String temp = props.getProperty("sndList");
			if (temp != null && ! temp.equals("")) {
				sndList = temp.split(MsgConstants.MSG_SEPARATOR);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sndList;
	}
	
	public void addSndList(String value) throws Exception {
		String temp = props.getProperty("sndList");
		if (temp == null || temp.equals("")) {
			temp = value;
		} else {
			temp = temp + MsgConstants.MSG_SEPARATOR + value;
		}
		props.setProperty("sndList", temp);
		outputConfFile();
	}
	
	public void setSndList(String[] value) throws Exception {
		String temp = "";
		for (int i = 0 ; i < value.length ; i++) {
			if (value[i] != null && ! value[i].equals("")) {
				if (i == 0) {
					temp = value[i];
				} else {
					temp = temp + MsgConstants.MSG_SEPARATOR + value[i];
				}
			}
		}
		MsgManager.getInstance().setSndList(value);
		props.setProperty("sndList", temp);
		outputConfFile();
	}
	
	public String[] getNwList() throws Exception {
		String[] sndList = null;
		try {
			String temp = props.getProperty("nwList");
			if (temp != null && ! temp.equals("")) {
				sndList = temp.split(MsgConstants.MSG_SEPARATOR);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sndList;
	}
		
	public void setNwList(String[] value) throws Exception {
		String temp = "";
		for (int i = 0 ; i < value.length ; i++) {
			if (value[i] != null && ! value[i].equals("")) {
				if (i == 0) {
					temp = value[i];
				} else {
					temp = temp + MsgConstants.MSG_SEPARATOR + value[i];
				}
			}
		}
		MsgManager.getInstance().setSndList(value);
		props.setProperty("nwList", temp);
		outputConfFile();
	}
	
	public String getNwGrp(){
		return props.getProperty("nwGrp");
	}
	
	public void setNwGrp(String value) throws Exception {
		props.setProperty("nwGrp", value);
		outputConfFile();
	}
	
	private void outputConfFile() throws Exception {
		if (fileCheck()) {
			props.store(new FileOutputStream(System.getProperty("user.home") + "/.msgshooter/conf/msg.properties"), "Configuration");
		}
	}
	private boolean fileCheck() {
		
		File file = new File(System.getProperty("user.home") + "/.msgshooter/conf/msg.properties");
		if (file.isFile()) {
			System.out.println("OK");
			if (file.canRead()) {
				return true;
			} else {
				System.err.println(" msg.properties read error! ");
			}
		} else {
			try {
				System.out.println("Not Fount msg.properties");
				File msgDir = new File("msg");
				if (! msgDir.isDirectory()) {
					if (msgDir.mkdir()) {
						file.createNewFile();	
						return true;
					} else {
						System.err.println(" mkdir conf error! ");						
					}
				} else {
					file.createNewFile();	
					return true;
				}
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}
		}
		return false;
	}
}